﻿namespace Bll.Stock
{
    using Bll.Comm;
    using Bll.Processing;
    using Bll.Procurement;
    using Bll.Sales;
    using IDal.Stock;
    using Model.Comm;
    using Model.Processing;
    using Model.Procurement;
    using Model.Sales;
    using Model.Stock;
    using Model.Systems;
    using System;
    using System.Data;
    using WinFormSession;
    using DALFactory;
    public class StockBll
    {
        private static readonly IStock dal = DataAccess.CreateStock();

        public static void Add(StockEnt model)
        {
            dal.Add(model);
        }

        public static void Delete(int ID)
        {
            dal.Delete(ID);
        }

        public static DataTable GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        public static int GetMaxId(string swhere)
        {
            return dal.GetMaxId(swhere);
        }

        public static StockEnt GetModel(int ID)
        {
            return dal.GetModel(ID);
        }

        public static DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetPageList(PageSize, PageIndex, strWhere);
        }

        public static int GetStockId(StockEnt stock, string ChainNum)
        {
            DataTable list = GetList(string.Concat(new object[] { "ChainNum=", ChainNum, " and LotNum='", stock.LotNum, "' and CommId=", stock.CommId, " and dlid=", stock.DlId }));
            if (list.Rows.Count > 0)
            {
                return int.Parse(list.Rows[0]["StockId"].ToString());
            }
            return 0;
        }

        public static int GetStockIdTwo(StockEnt stock, string ChainNum)
        {
            DataTable list = GetList(string.Concat(new object[] { "ChainNum=", ChainNum, " and LotNum='", stock.LotNum, "' and CommId=", stock.CommId, " and dlid=", stock.DlId, " and AmoutUnit=", stock.AmoutUnit }));
            if (list.Rows.Count > 0)
            {
                return int.Parse(list.Rows[0]["StockId"].ToString());
            }
            return 0;
        }

        public static bool InputDll(string ProcurementId)
        {
            try
            {
                UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
                ProcurementEnt model = ProcurementBll.GetModel(int.Parse(ProcurementId));
                DataTable list = ProcurementCommBuyBll.GetList("ProcurementId=" + ProcurementId);
                decimal[] numArray = new decimal[list.Rows.Count];
                decimal[] numArray2 = new decimal[list.Rows.Count];
                int index = 0;
                foreach (DataRow row in list.Rows)
                {
                    StockEnt stock = new StockEnt();
                    CommEnt ent4 = CommBll.GetModel(int.Parse(row["CommId"].ToString()));
                    stock.ChainNum = ent.ChainNum;
                    stock.DlId = ent4.CommDefaultDlId;
                    stock.LotNum = row["LotNum"].ToString();
                    stock.ProcurementId = int.Parse(ProcurementId);
                    stock.SupplierListId = model.SupplierId.ToString();
                    stock.CommTypeId = ent4.CommTypeId;
                    stock.CommId = int.Parse(row["CommId"].ToString());
                    stock.CommName = row["CommName"].ToString();
                    stock.Amount = decimal.Parse(row["Amount"].ToString());
                    stock.AmoutUnit = int.Parse(row["AmoutUnit"].ToString());
                    stock.AmoutMoney = decimal.Parse(row["AmoutMoney"].ToString());
                    stock.TimeExpired = DateTime.Now;
                    int stockId = GetStockId(stock, ent.ChainNum.ToString());
                    if (stockId == 0)
                    {
                        numArray[index] = stock.Amount;
                        numArray2[index] = stock.AmoutMoney;
                        Add(stock);
                    }
                    else
                    {
                        StockEnt ent5 = GetModel(stockId);
                        ent5.Amount += stock.Amount;
                        ent5.AmoutMoney = (ent5.AmoutMoney + stock.AmoutMoney) / 2M;
                        ent5.TimeExpired = DateTime.Now;
                        numArray[index] = ent5.Amount;
                        numArray2[index] = ent5.AmoutMoney * ent5.Amount;
                        Update(ent5);
                    }
                    index++;
                }
                GoodsInOutEnt ent6 = new GoodsInOutEnt {
                    ChainNum = ent.ChainNum,
                    DetailsType = 0,
                    LibraryTypeId = 0x22,
                    UserId = ent.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(ent6);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent.ChainNum);
                int num4 = 0;
                foreach (DataRow row in list.Rows)
                {
                    LibraryDetailsCommEnt ent7 = new LibraryDetailsCommEnt {
                        DetailsId = maxId,
                        LotNum = row["LotNum"].ToString(),
                        ChainNum = ent.ChainNum,
                        SupplierId = model.SupplierId.ToString(),
                        CommId = int.Parse(row["CommId"].ToString()),
                        CommName = row["CommName"].ToString(),
                        DlId = int.Parse(row["DllId"].ToString()),
                        InputPrice = decimal.Parse(row["AmoutMoney"].ToString()),
                        Amount = decimal.Parse(row["Amount"].ToString()),
                        AmoutUnit = int.Parse(row["AmoutUnit"].ToString()),
                        Remark = row["Features"].ToString(),
                        ResidualMoney = numArray2[num4],
                        ResidualNum = numArray[num4]
                    };
                    LibraryDetailsCommBll.Add(ent7);
                    num4++;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static int OutDll(string orderId)
        {
            int num = 0;
            OrdersEnt model = OrdersBll.GetModel(int.Parse(orderId));
            DataTable list = OrdersCommBll.GetList("OrdersId=" + orderId);
            decimal[] numArray = new decimal[list.Rows.Count];
            decimal[] numArray2 = new decimal[list.Rows.Count];
            int index = 0;
            UserEnt ent2 = (UserEnt) WinformSession.GetSession()["user"];
            foreach (DataRow row in list.Rows)
            {
                StockEnt stock = new StockEnt {
                    DlId = int.Parse(row["DlId"].ToString()),
                    ChainNum = ent2.ChainNum,
                    LotNum = row["LotNum"].ToString(),
                    CommId = int.Parse(row["CommId"].ToString()),
                    CommName = row["CommName"].ToString(),
                    Amount = decimal.Parse(row["Amount"].ToString()),
                    AmoutUnit = int.Parse(row["AmoutUnit"].ToString()),
                    AmoutMoney = decimal.Parse(row["AmoutMoney"].ToString())
                };
                bool flag = false;
                int stockId = GetStockId(stock, ent2.ChainNum.ToString());
                if (stockId == 0)
                {
                    return 2;
                }
                StockEnt ent4 = GetModel(stockId);
                if (ent4.Amount < stock.Amount)
                {
                    return 1;
                }
                if (ent4.Amount == stock.Amount)
                {
                    flag = true;
                }
                else
                {
                    ent4.Amount -= stock.Amount;
                }
                Update(ent4);
                numArray[index] = ent4.Amount;
                numArray2[index] = ent4.Amount * ent4.AmoutMoney;
                if (flag)
                {
                    Delete(stockId);
                }
                num = 0;
                index++;
            }
            GoodsInOutEnt ent5 = new GoodsInOutEnt {
                ChainNum = ent2.ChainNum,
                DetailsType = 1,
                LibraryTypeId = 0x26,
                UserId = ent2.UserId,
                CreateTime = DateTime.Now
            };
            GoodsInOutBll.Add(ent5);
            int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent2.ChainNum);
            int num5 = 0;
            foreach (DataRow row in list.Rows)
            {
                LibraryDetailsCommEnt ent6 = new LibraryDetailsCommEnt {
                    DetailsId = maxId,
                    LotNum = row["LotNum"].ToString(),
                    ChainNum = ent2.ChainNum,
                    SupplierId = row["SupplierId"].ToString(),
                    CommId = int.Parse(row["CommId"].ToString()),
                    CommName = row["CommName"].ToString(),
                    DlId = int.Parse(row["DlId"].ToString()),
                    InputPrice = decimal.Parse(row["AmoutMoney"].ToString()),
                    Amount = decimal.Parse(row["Amount"].ToString()),
                    AmoutUnit = int.Parse(row["AmoutUnit"].ToString()),
                    Remark = row["Remark"].ToString(),
                    ResidualNum = numArray[num5],
                    ResidualMoney = numArray2[num5]
                };
                LibraryDetailsCommBll.Add(ent6);
                num5++;
            }
            return num;
        }

        public static int ProReturn(int ProcurementId)
        {
            int num6;
            try
            {
                int num = 0;
                UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
                ProcurementEnt model = ProcurementBll.GetModel(ProcurementId);
                DataTable list = ProcurementCommBuyBll.GetList("ProcurementId=" + ProcurementId.ToString());
                decimal[] numArray = new decimal[list.Rows.Count];
                decimal[] numArray2 = new decimal[list.Rows.Count];
                int index = 0;
                foreach (DataRow row in list.Rows)
                {
                    StockEnt stock = new StockEnt();
                    CommEnt ent4 = CommBll.GetModel(int.Parse(row["CommId"].ToString()));
                    if (ent4 == null)
                    {
                        return 2;
                    }
                    stock.ChainNum = ent.ChainNum;
                    stock.DlId = ent4.CommDefaultDlId;
                    stock.LotNum = row["LotNum"].ToString();
                    stock.ProcurementId = ProcurementId;
                    stock.SupplierListId = model.SupplierId.ToString();
                    stock.CommTypeId = ent4.CommTypeId;
                    stock.CommId = int.Parse(row["CommId"].ToString());
                    stock.CommName = row["CommName"].ToString();
                    stock.Amount = decimal.Parse(row["Amount"].ToString());
                    stock.AmoutUnit = int.Parse(row["AmoutUnit"].ToString());
                    stock.AmoutMoney = decimal.Parse(row["AmoutMoney"].ToString());
                    stock.TimeExpired = DateTime.Now;
                    bool flag = false;
                    int stockId = GetStockId(stock, ent.ChainNum.ToString());
                    if (stockId == 0)
                    {
                        return 2;
                    }
                    StockEnt ent5 = GetModel(stockId);
                    if (ent5.Amount < stock.Amount)
                    {
                        return 1;
                    }
                    if (ent5.Amount == stock.Amount)
                    {
                        flag = true;
                    }
                    else
                    {
                        ent5.Amount -= stock.Amount;
                    }
                    Update(ent5);
                    numArray[index] = ent5.Amount;
                    numArray2[index] = ent5.Amount * ent5.AmoutMoney;
                    if (flag)
                    {
                        Delete(stockId);
                    }
                    num = 0;
                    index++;
                }
                GoodsInOutEnt ent6 = new GoodsInOutEnt {
                    ChainNum = ent.ChainNum,
                    DetailsType = 1,
                    LibraryTypeId = 0x26,
                    UserId = ent.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(ent6);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent.ChainNum);
                int num5 = 0;
                foreach (DataRow row in list.Rows)
                {
                    LibraryDetailsCommEnt ent7 = new LibraryDetailsCommEnt {
                        DetailsId = maxId,
                        LotNum = row["LotNum"].ToString(),
                        ChainNum = ent.ChainNum,
                        SupplierId = model.SupplierId.ToString(),
                        CommId = int.Parse(row["CommId"].ToString()),
                        CommName = row["CommName"].ToString(),
                        DlId = int.Parse(row["DllId"].ToString()),
                        InputPrice = decimal.Parse(row["AmoutMoney"].ToString()),
                        Amount = decimal.Parse(row["Amount"].ToString()),
                        AmoutUnit = int.Parse(row["AmoutUnit"].ToString()),
                        Remark = row["Remark"].ToString(),
                        ResidualNum = numArray[num5],
                        ResidualMoney = numArray2[num5]
                    };
                    LibraryDetailsCommBll.Add(ent7);
                    num5++;
                }
                num6 = num;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return num6;
        }

        public static bool SalesReturn(int OrderId)
        {
            try
            {
                UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
                OrdersEnt model = OrdersBll.GetModel(OrderId);
                DataTable list = OrdersCommBll.GetList("OrdersId=" + OrderId);
                decimal[] numArray = new decimal[list.Rows.Count];
                decimal[] numArray2 = new decimal[list.Rows.Count];
                int index = 0;
                foreach (DataRow row in list.Rows)
                {
                    StockEnt stock = new StockEnt();
                    CommEnt ent4 = CommBll.GetModel(int.Parse(row["CommId"].ToString()));
                    stock.ChainNum = ent.ChainNum;
                    stock.DlId = ent4.CommDefaultDlId;
                    stock.LotNum = row["LotNum"].ToString();
                    stock.ProcurementId = 0;
                    stock.SupplierListId = "";
                    stock.CommTypeId = ent4.CommTypeId;
                    stock.CommId = int.Parse(row["CommId"].ToString());
                    stock.CommName = row["CommName"].ToString();
                    stock.Amount = decimal.Parse(row["Amount"].ToString());
                    stock.AmoutUnit = int.Parse(row["AmoutUnit"].ToString());
                    stock.AmoutMoney = decimal.Parse(row["Cost"].ToString());
                    stock.TimeExpired = DateTime.Now;
                    int stockId = GetStockId(stock, ent.ChainNum.ToString());
                    if (stockId == 0)
                    {
                        numArray[index] = stock.Amount;
                        numArray2[index] = stock.AmoutMoney;
                        Add(stock);
                    }
                    else
                    {
                        StockEnt ent5 = GetModel(stockId);
                        ent5.Amount += stock.Amount;
                        ent5.AmoutMoney = (ent5.AmoutMoney + stock.AmoutMoney) / 2M;
                        ent5.TimeExpired = DateTime.Now;
                        numArray[index] = ent5.Amount;
                        numArray2[index] = ent5.AmoutMoney * ent5.Amount;
                        Update(ent5);
                    }
                    index++;
                }
                GoodsInOutEnt ent6 = new GoodsInOutEnt {
                    ChainNum = ent.ChainNum,
                    DetailsType = 0,
                    LibraryTypeId = 0x23,
                    UserId = ent.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(ent6);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent.ChainNum);
                int num4 = 0;
                foreach (DataRow row in list.Rows)
                {
                    LibraryDetailsCommEnt ent7 = new LibraryDetailsCommEnt {
                        DetailsId = maxId,
                        LotNum = row["LotNum"].ToString(),
                        ChainNum = ent.ChainNum,
                        SupplierId = "",
                        CommId = int.Parse(row["CommId"].ToString()),
                        CommName = row["CommName"].ToString(),
                        DlId = int.Parse(row["DlId"].ToString()),
                        InputPrice = decimal.Parse(row["Cost"].ToString()),
                        Amount = decimal.Parse(row["Amount"].ToString()),
                        AmoutUnit = int.Parse(row["AmoutUnit"].ToString()),
                        Remark = row["Remark"].ToString(),
                        ResidualMoney = numArray2[num4],
                        ResidualNum = numArray[num4]
                    };
                    LibraryDetailsCommBll.Add(ent7);
                    num4++;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool TeaMixedInput(string MixedId)
        {
            if (MixedId != "")
            {
                decimal amount = 0M;
                decimal amoutMoney = 0M;
                TeaMixedEnt model = TeaMixedBll.GetModel(int.Parse(MixedId));
                StockEnt stock = new StockEnt();
                UserEnt ent3 = (UserEnt) WinformSession.GetSession()["user"];
                stock.DlId = model.NewTeaDlid;
                stock.LotNum = model.LotNum;
                stock.ProcurementId = 0;
                stock.SupplierListId = "";
                CommEnt ent4 = CommBll.GetModel(model.CommId);
                stock.CommTypeId = ent4.CommTypeId;
                stock.CommId = model.CommId;
                stock.CommName = model.CommName;
                stock.Amount = model.NewTeaWeight;
                stock.AmoutUnit = model.CommUnit;
                stock.AmoutMoney = model.CommPrice;
                stock.TimeExpired = DateTime.Now;
                int stockId = GetStockId(stock, ent3.ChainNum.ToString());
                if (stockId == 0)
                {
                    amount = stock.Amount;
                    amoutMoney = stock.AmoutMoney;
                    stock.ChainNum = ent3.ChainNum;
                    Add(stock);
                }
                else
                {
                    StockEnt ent5 = GetModel(stockId);
                    ent5.Amount += ent5.Amount;
                    ent5.AmoutMoney = (ent5.AmoutMoney + ent5.AmoutMoney) % 2M;
                    ent5.TimeExpired = DateTime.Now;
                    amount = ent5.Amount;
                    amoutMoney = ent5.AmoutMoney * ent5.Amount;
                    Update(ent5);
                }
                GoodsInOutEnt ent6 = new GoodsInOutEnt {
                    ChainNum = ent3.ChainNum,
                    DetailsType = 0,
                    LibraryTypeId = 0x31,
                    UserId = ent3.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(ent6);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent3.ChainNum);
                LibraryDetailsCommEnt ent7 = new LibraryDetailsCommEnt {
                    DetailsId = maxId,
                    LotNum = stock.LotNum,
                    ChainNum = ent3.ChainNum,
                    SupplierId = stock.SupplierListId,
                    CommId = stock.CommId,
                    CommName = stock.CommName,
                    DlId = stock.DlId,
                    InputPrice = stock.AmoutMoney,
                    Amount = stock.Amount,
                    AmoutUnit = stock.AmoutUnit,
                    Remark = "",
                    ResidualMoney = amoutMoney,
                    ResidualNum = amount
                };
                LibraryDetailsCommBll.Add(ent7);
                return true;
            }
            return false;
        }

        public static bool TeaMixedOut(string MixedId)
        {
            if (MixedId != "")
            {
                DataTable list = TeaMixedCommBll.GetList("Mixed=" + MixedId);
                string str = "";
                decimal[] numArray = new decimal[list.Rows.Count];
                decimal[] numArray2 = new decimal[list.Rows.Count];
                int index = 0;
                bool flag = false;
                foreach (DataRow row in list.Rows)
                {
                    StockEnt ent = GetModel(int.Parse(row["stockId"].ToString()));
                    decimal num2 = decimal.Parse(row["Weight"].ToString());
                    if (ent.Amount > num2)
                    {
                        ent.Amount -= num2;
                        numArray[index] = ent.Amount;
                        numArray2[index] = ent.Amount * ent.AmoutMoney;
                        Update(ent);
                        str = str + ent.SupplierListId + ",";
                    }
                    else if (ent.Amount == num2)
                    {
                        flag = true;
                    }
                    else
                    {
                        return false;
                    }
                    index++;
                    if (flag)
                    {
                        Delete(ent.StockId);
                    }
                }
                UserEnt ent2 = (UserEnt) WinformSession.GetSession()["user"];
                GoodsInOutEnt model = new GoodsInOutEnt {
                    ChainNum = ent2.ChainNum,
                    DetailsType = 1,
                    LibraryTypeId = 50,
                    UserId = ent2.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(model);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent2.ChainNum);
                int num4 = 0;
                foreach (DataRow row in list.Rows)
                {
                    LibraryDetailsCommEnt ent4 = new LibraryDetailsCommEnt {
                        DetailsId = maxId
                    };
                    StockEnt ent5 = GetModel(int.Parse(row["stockid"].ToString()));
                    ent4.LotNum = ent5.LotNum;
                    ent4.SupplierId = ent5.SupplierListId;
                    ent4.ChainNum = ent2.ChainNum;
                    ent4.CommId = ent5.CommId;
                    ent4.CommName = ent5.CommName;
                    ent4.DlId = ent5.DlId;
                    ent4.InputPrice = ent5.AmoutMoney;
                    ent4.Amount = decimal.Parse(row["Weight"].ToString());
                    ent4.AmoutUnit = int.Parse(row["CommUnit"].ToString());
                    ent4.Remark = "";
                    ent4.ResidualNum = numArray[num4];
                    ent4.ResidualMoney = numArray2[num4];
                    LibraryDetailsCommBll.Add(ent4);
                    num4++;
                }
                return true;
            }
            return false;
        }

        public static bool TeaPackagingInput(string PackagingId)
        {
            if (PackagingId != "")
            {
                decimal amount = 0M;
                decimal amoutMoney = 0M;
                UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
                TeaPackagingEnt model = TeaPackagingBll.GetModel(int.Parse(PackagingId));
                StockEnt stock = new StockEnt {
                    DlId = model.TeaPackagingInputDlId,
                    ProcurementId = 0,
                    SupplierListId = "0"
                };
                CommEnt ent4 = CommBll.GetModel(model.TeaCommId);
                stock.CommTypeId = ent4.CommTypeId;
                stock.CommId = model.TeaCommId;
                stock.CommName = model.TeaCommName;
                stock.LotNum = model.TeaCommLots;
                stock.Amount = model.TeaPackagingNum;
                stock.AmoutUnit = int.Parse(ent4.CommUnit);
                stock.AmoutMoney = decimal.Parse(model.TeaPackagingMoney);
                stock.TimeExpired = DateTime.Now;
                int stockIdTwo = GetStockIdTwo(stock, ent.ChainNum.ToString());
                if (stockIdTwo == 0)
                {
                    amount = stock.Amount;
                    amoutMoney = stock.AmoutMoney;
                    stock.ChainNum = ent.ChainNum;
                    Add(stock);
                }
                else
                {
                    StockEnt ent5 = GetModel(stockIdTwo);
                    ent5.Amount += stock.Amount;
                    ent5.AmoutMoney = (ent5.AmoutMoney + stock.AmoutMoney) % 2M;
                    ent5.TimeExpired = DateTime.Now;
                    amount = ent5.Amount;
                    amoutMoney = ent5.AmoutMoney * ent5.Amount;
                    Update(ent5);
                }
                GoodsInOutEnt ent6 = new GoodsInOutEnt {
                    ChainNum = ent.ChainNum,
                    DetailsType = 0,
                    LibraryTypeId = 0x33,
                    UserId = ent.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(ent6);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent.ChainNum);
                LibraryDetailsCommEnt ent7 = new LibraryDetailsCommEnt {
                    DetailsId = maxId,
                    LotNum = stock.LotNum,
                    ChainNum = ent.ChainNum,
                    SupplierId = stock.SupplierListId,
                    CommId = stock.CommId,
                    CommName = stock.CommName,
                    DlId = stock.DlId,
                    InputPrice = stock.AmoutMoney,
                    Amount = stock.Amount,
                    AmoutUnit = stock.AmoutUnit,
                    Remark = "",
                    ResidualMoney = amoutMoney,
                    ResidualNum = amount
                };
                LibraryDetailsCommBll.Add(ent7);
                return true;
            }
            return false;
        }

        public static string TeaPackagingOut(string PackagingId)
        {
            if (PackagingId != "")
            {
                DataTable list = TeaPackagingCommBll.GetList("PackagingId=" + PackagingId);
                string str = "";
                int index = 0;
                TeaPackagingEnt model = TeaPackagingBll.GetModel(int.Parse(PackagingId));
                decimal[] numArray = new decimal[list.Rows.Count];
                decimal[] numArray2 = new decimal[list.Rows.Count];
                UserEnt ent2 = (UserEnt) WinformSession.GetSession()["user"];
                bool flag = false;
                foreach (DataRow row in list.Rows)
                {
                    StockEnt ent3 = GetModel(int.Parse(row["stockId"].ToString()));
                    decimal num2 = decimal.Parse(row["Weight"].ToString());
                    if (ent3.Amount > num2)
                    {
                        ent3.Amount -= num2;
                        numArray[index] = ent3.Amount;
                        numArray2[index] = ent3.Amount * ent3.AmoutMoney;
                        Update(ent3);
                        str = str + ent3.SupplierListId + ",";
                    }
                    else if (ent3.Amount == num2)
                    {
                        flag = true;
                    }
                    else
                    {
                        return (ent3.CommName + " 库存数量不够");
                    }
                    index++;
                    if (flag)
                    {
                        Delete(ent3.StockId);
                    }
                }
                GoodsInOutEnt ent4 = new GoodsInOutEnt {
                    ChainNum = ent2.ChainNum,
                    DetailsType = 1,
                    LibraryTypeId = 0x34,
                    UserId = ent2.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(ent4);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent2.ChainNum);
                int num4 = 0;
                foreach (DataRow row in list.Rows)
                {
                    LibraryDetailsCommEnt ent5 = new LibraryDetailsCommEnt {
                        DetailsId = maxId
                    };
                    StockEnt ent6 = GetModel(int.Parse(row["stockid"].ToString()));
                    ent5.LotNum = ent6.LotNum;
                    ent5.ChainNum = ent2.ChainNum;
                    ent5.SupplierId = ent6.SupplierListId;
                    ent5.CommId = ent6.CommId;
                    ent5.CommName = ent6.CommName;
                    ent5.DlId = ent6.DlId;
                    ent5.InputPrice = ent6.AmoutMoney;
                    ent5.Amount = decimal.Parse(row["Weight"].ToString());
                    ent5.AmoutUnit = int.Parse(row["CommUnit"].ToString());
                    ent5.Remark = "";
                    ent5.ResidualNum = numArray[num4];
                    ent5.ResidualMoney = numArray2[num4];
                    LibraryDetailsCommBll.Add(ent5);
                    num4++;
                }
                return "";
            }
            return "物品出库失败";
        }

        public static bool TeaProcessingInput(string ProcessingId)
        {
            try
            {
                UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
                TeaProcessingEnt model = TeaProcessingBll.GetModel(int.Parse(ProcessingId));
                DataTable list = TeaProcessingCommBll.GetList("ProcessingCommId=" + ProcessingId);
                decimal[] numArray = new decimal[list.Rows.Count];
                decimal[] numArray2 = new decimal[list.Rows.Count];
                int index = 0;
                foreach (DataRow row in list.Rows)
                {
                    StockEnt stock = new StockEnt {
                        ChainNum = ent.ChainNum,
                        DlId = int.Parse(row["DllId"].ToString()),
                        LotNum = row["LotNum"].ToString(),
                        ProcurementId = 0
                    };
                    CommEnt ent4 = CommBll.GetModel(int.Parse(row["CommId"].ToString()));
                    stock.CommTypeId = ent4.CommTypeId;
                    stock.CommId = int.Parse(row["CommId"].ToString());
                    stock.CommName = row["CommName"].ToString();
                    stock.Amount = decimal.Parse(row["Weight"].ToString());
                    stock.AmoutUnit = int.Parse(row["CommUnit"].ToString());
                    stock.AmoutMoney = decimal.Parse(row["CommMoney"].ToString());
                    stock.TimeExpired = DateTime.Now;
                    int stockId = GetStockId(stock, ent.ChainNum.ToString());
                    if (stockId == 0)
                    {
                        numArray[index] = stock.Amount;
                        numArray2[index] = stock.AmoutMoney;
                        Add(stock);
                    }
                    else
                    {
                        StockEnt ent5 = GetModel(stockId);
                        ent5.Amount += stock.Amount;
                        ent5.AmoutMoney = (ent5.AmoutMoney + stock.AmoutMoney) % 2M;
                        ent5.TimeExpired = DateTime.Now;
                        numArray[index] = ent5.Amount;
                        numArray2[index] = ent5.AmoutMoney * ent5.Amount;
                        Update(ent5);
                    }
                    index++;
                }
                GoodsInOutEnt ent6 = new GoodsInOutEnt {
                    ChainNum = ent.ChainNum,
                    DetailsType = 0,
                    LibraryTypeId = 0x2f,
                    UserId = ent.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(ent6);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent.ChainNum);
                int num4 = 0;
                foreach (DataRow row in list.Rows)
                {
                    LibraryDetailsCommEnt ent7 = new LibraryDetailsCommEnt {
                        DetailsId = maxId,
                        LotNum = row["LotNum"].ToString(),
                        ChainNum = ent.ChainNum,
                        SupplierId = "0",
                        CommId = int.Parse(row["CommId"].ToString()),
                        CommName = row["CommName"].ToString(),
                        DlId = int.Parse(row["DllId"].ToString()),
                        InputPrice = decimal.Parse(row["CommMoney"].ToString()),
                        Amount = decimal.Parse(row["Weight"].ToString()),
                        AmoutUnit = int.Parse(row["CommUnit"].ToString()),
                        Remark = "",
                        ResidualMoney = numArray2[num4],
                        ResidualNum = numArray[num4]
                    };
                    LibraryDetailsCommBll.Add(ent7);
                    num4++;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool TeaProcessingOutput(string ProcessingId)
        {
            TeaProcessingEnt model = TeaProcessingBll.GetModel(int.Parse(ProcessingId));
            if (model != null)
            {
                decimal amount = 0M;
                decimal num2 = 0M;
                UserEnt ent2 = (UserEnt) WinformSession.GetSession()["user"];
                StockEnt ent3 = GetModel(model.StockId);
                bool flag = false;
                decimal weight = model.Weight;
                if (ent3.Amount > weight)
                {
                    ent3.Amount -= weight;
                    amount = ent3.Amount;
                    num2 = ent3.Amount * ent3.AmoutMoney;
                    Update(ent3);
                }
                else if (ent3.Amount == weight)
                {
                    flag = true;
                }
                else
                {
                    return false;
                }
                if (flag)
                {
                    Delete(ent3.StockId);
                }
                GoodsInOutEnt ent4 = new GoodsInOutEnt {
                    ChainNum = ent2.ChainNum,
                    DetailsType = 1,
                    LibraryTypeId = 0x30,
                    UserId = ent2.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(ent4);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent2.ChainNum);
                LibraryDetailsCommEnt ent5 = new LibraryDetailsCommEnt {
                    DetailsId = maxId,
                    LotNum = ent3.LotNum,
                    ChainNum = ent2.ChainNum,
                    SupplierId = ent3.SupplierListId,
                    CommId = ent3.CommId,
                    CommName = ent3.CommName,
                    DlId = ent3.DlId,
                    InputPrice = ent3.AmoutMoney,
                    Amount = weight,
                    AmoutUnit = ent3.AmoutUnit,
                    Remark = "",
                    ResidualNum = amount,
                    ResidualMoney = num2
                };
                LibraryDetailsCommBll.Add(ent5);
                return true;
            }
            return false;
        }

        public static int TestDunkOutPut(string TestDunkId)
        {
            TestDunkEnt model = TestDunkBll.GetModel(int.Parse(TestDunkId));
            int num = 1;
            if (model != null)
            {
                DataTable list = TestDunkCommBll.GetList("TestDunkId=" + model.TestDunkId);
                decimal[] numArray = new decimal[list.Rows.Count];
                decimal[] numArray2 = new decimal[list.Rows.Count];
                int index = 0;
                UserEnt ent2 = (UserEnt) WinformSession.GetSession()["user"];
                foreach (DataRow row in list.Rows)
                {
                    StockEnt stock = new StockEnt {
                        DlId = int.Parse(row["DlId"].ToString()),
                        ChainNum = ent2.ChainNum,
                        LotNum = row["LotNum"].ToString(),
                        CommId = int.Parse(row["CommId"].ToString()),
                        CommName = row["CommName"].ToString(),
                        Amount = decimal.Parse(row["Amount"].ToString()),
                        AmoutUnit = int.Parse(row["AmoutUnit"].ToString()),
                        AmoutMoney = decimal.Parse(row["Cost"].ToString())
                    };
                    bool flag = false;
                    int stockId = GetStockId(stock, ent2.ChainNum.ToString());
                    if (stockId == 0)
                    {
                        return 2;
                    }
                    StockEnt ent4 = GetModel(stockId);
                    if (ent4.Amount < stock.Amount)
                    {
                        return 1;
                    }
                    if (ent4.Amount == stock.Amount)
                    {
                        flag = true;
                    }
                    else
                    {
                        ent4.Amount -= stock.Amount;
                    }
                    Update(ent4);
                    numArray[index] = ent4.Amount;
                    numArray2[index] = ent4.Amount * ent4.AmoutMoney;
                    if (flag)
                    {
                        Delete(stockId);
                    }
                    num = 0;
                    index++;
                }
                GoodsInOutEnt ent5 = new GoodsInOutEnt {
                    ChainNum = ent2.ChainNum,
                    DetailsType = 1,
                    LibraryTypeId = 0x4d,
                    UserId = ent2.UserId,
                    CreateTime = DateTime.Now
                };
                GoodsInOutBll.Add(ent5);
                int maxId = GoodsInOutBll.GetMaxId("ChainNum=" + ent2.ChainNum);
                int num5 = 0;
                foreach (DataRow row in list.Rows)
                {
                    LibraryDetailsCommEnt ent6 = new LibraryDetailsCommEnt {
                        DetailsId = maxId,
                        LotNum = row["LotNum"].ToString(),
                        SupplierId = row["SupplierId"].ToString(),
                        CommId = int.Parse(row["CommId"].ToString()),
                        CommName = row["CommName"].ToString(),
                        DlId = int.Parse(row["DlId"].ToString()),
                        InputPrice = decimal.Parse(row["Cost"].ToString()),
                        Amount = decimal.Parse(row["Amount"].ToString()),
                        AmoutUnit = int.Parse(row["AmoutUnit"].ToString()),
                        Remark = row["Remark"].ToString(),
                        ResidualNum = numArray[num5],
                        ResidualMoney = numArray2[num5]
                    };
                    LibraryDetailsCommBll.Add(ent6);
                    num5++;
                }
            }
            return num;
        }

        public static void Update(StockEnt model)
        {
            dal.Update(model);
        }
    }
}

